using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

using Impact.Controllers;
using Impact.Infrastructure.Plugins;
using Impact.Model.Objects;
using Impact.Web.Foundation;

namespace Impact.Web.Admin
{
    /// <summary>
    /// code-behind for plugin parameters editing page
    /// </summary>
    public partial class EditPluginParameters : PageBase
    {
        private PagePlugin plugin;

        /// <summary>
        /// override for onload to perform startup functionality
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            string regId = Request.QueryString["rid"];
            if (!string.IsNullOrEmpty(regId))
            {
                plugin = new PagePlugin(Int32.Parse(regId));
            }
            else
            {
                throw new ArgumentException("Registration ID is required on the querystring>");
            }


            // we want to bind all the time, so handle the condition that base() doesn't
            if (IsPostBack) Bind();

            base.OnLoad(e);

            // run this after base.OnLoad() to override the base class setting
            string returnUrl = Request.QueryString["ReturnUrl"];
            if (!string.IsNullOrEmpty(returnUrl))
            {
                ReferringUrl = returnUrl;
            }


        }

        /// <summary>
        /// overridden to handling binding on this page
        /// </summary>
        protected override void Bind()
        {
            this.pluginName.Text = plugin.Plugin.Name;
            this.parameterRepeater.DataSource = plugin.GetParameters();
            this.parameterRepeater.DataBind();
        }


        /// <summary>
        /// event handler for when an item in the plugin-parameter repeater is data bound
        /// </summary>
        protected void parameterRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            KeyValuePair<string, PluginParameter> keyValue = (KeyValuePair<string, PluginParameter>)e.Item.DataItem;
            PluginParameter parameter = keyValue.Value;

            PlaceHolder valuePlaceHolder = (PlaceHolder)e.Item.FindControl("valuePlaceHolder");

            Control control;
            if (parameter.Plugin.Admin != null)
            {
                control = parameter.Plugin.Admin.GetParameterEditControl(parameter);
            }
            else
            {
                // default to textBox
                TextBox textBox = new TextBox();
                textBox.Text = parameter.Value;
                control = textBox;
            }
            control.ID = "value";
            valuePlaceHolder.Controls.Add(control);

        }

        /// <summary>
        /// event handler fired when the user clicks submit
        /// </summary>
        protected void Submit_Command(object sender, EventArgs e)
        {
            SaveData();
            Return();
        }

        /// <summary>
        /// event handler fired when the user clicks cancel
        /// </summary>
        protected void Cancel_Command(object sender, EventArgs e)
        {
            Return();
        }

        /// <summary>
        /// helper to save the settings in the parameters back to the plugin
        /// </summary>
        private void SaveData()
        {
            Dictionary<string, PluginParameter> parameters = plugin.GetParameters();

            foreach (RepeaterItem item in parameterRepeater.Items)
            {
                Label nameLabel = (Label)item.FindControl("name");
                PluginParameter parameter = parameters[nameLabel.Text];
                PlaceHolder valuePlaceHolder = (PlaceHolder)item.FindControl("valuePlaceHolder");
                Control valueControl = valuePlaceHolder.FindControl("value");

                if (parameter.Plugin.Admin != null)
                {
                    parameter.Value = parameter.Plugin.Admin.GetControlValue(valueControl);
                }
                // try default textBox
                else if (valueControl is TextBox)
                {
                    parameter.Value = ((TextBox)valueControl).Text;
                }

                parameters[nameLabel.Text] = parameter;
            }

            plugin.SetParameters( parameters );

            EditPluginController controller = new EditPluginController(plugin.PageId);
            controller.Save(plugin);

        }


    }
}
